package cn.ffcs.uec.wBStaffInfo.action;

import java.util.HashMap;
import java.util.Map;


import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.Components;
import org.zkoss.zk.ui.Executions;
import org.zkoss.zk.ui.event.Event;
import org.zkoss.zk.ui.event.EventListener;
import org.zkoss.zkplus.databind.BindingListModelList;
import org.zkoss.zul.ListModel;
import org.zkoss.zul.Messagebox;
import org.zkoss.zul.Window;

import cn.ffcs.raptornuke.plugin.common.zk.ctrl.BasePortletComposer;
import cn.ffcs.raptornuke.plugin.common.zk.util.ZkUtil;
import cn.ffcs.raptornuke.portal.theme.ThemeDisplay;
import cn.ffcs.uec.outStaff.manager.UecOutStaffManager;
import cn.ffcs.uec.outStaff.model.UecOutStaffInfo;
import cn.ffcs.uec.staff.manager.UecEncManager;
import cn.ffcs.uec.staff.manager.UecTrialManager;
import cn.ffcs.uec.staff.model.UecEnc;
import cn.ffcs.uec.staff.model.UecTrial;
import cn.ffcs.uec.wBStaffInfo.action.bean.WbEncListboxBean;
import cn.ffcs.uom.common.constants.BaseUnitConstants;
import cn.ffcs.uom.common.util.ApplicationContextUtil;
import cn.ffcs.uom.common.util.IPortletInfoProvider;
import cn.ffcs.uom.common.util.PlatformUtil;
import cn.ffcs.uom.common.util.StrUtil;
import cn.ffcs.uom.common.util.UomZkUtil;
import cn.ffcs.uom.common.vo.PageInfo;
import cn.ffcs.uom.common.zul.PubUtil;
import lombok.Getter;
import lombok.Setter;

/**
 * @Title:奖励信息
 * @Description:
 * @author BMP
 * @time 2019-01-10
 */
@Controller
@Scope("prototype")
public class WbEncMainComposer extends BasePortletComposer implements
IPortletInfoProvider {

	private static final long serialVersionUID = -5811398149406451671L;
	private String editEncUrl="/pages_nmg/uecOutStaffInfomation/WBEnc_edit.zul";
	/**
	 * 人员基本信息功能 查询出的实体对象
	 */
	private UecOutStaffInfo uecOutStaffInfo;
	/**
	 * 对应页面上的bean
	 */
	@Getter
	private WbEncListboxBean bean = new WbEncListboxBean();

	/**
	 * 人员奖励信息
	 */
	private UecEnc uecEnc;
	/**
	 * 翻页查询条件实体
	 */
	private UecEnc encPaging;


	private UecEncManager uecEncManager = (UecEncManager) ApplicationContextUtil
			.getBean("uecEncManager");

	private UecOutStaffManager uecOutStaffInfoManager = (UecOutStaffManager) ApplicationContextUtil
			.getBean("uecOutStaffManager");
	private UecTrialManager uecTrialManager = (UecTrialManager) ApplicationContextUtil
			.getBean("uecTrialManager");
	@Getter
	@Setter
	private IPortletInfoProvider portletInfoProvider;
	@Override
	public String getPortletId() {
		return super.getPortletId();
	}

	@Override
	public ThemeDisplay getThemeDisplay() {
		return super.getThemeDisplay();
	}

	@Override
	public void doAfterCompose(Component comp) throws Exception {
		super.doAfterCompose(comp);
		UomZkUtil.autoFitHeight(comp);
		Components.wireVariables(comp, bean);
	}
	/**
	 * 初始化
	 */
	public void onCreate$wbEncMainWin() {
		onUecOutStaffEncQueryResponse();
	}


	/**
	 * ，抛出事件， 事件监听
	 * 
	 * @param event
	 */
	public void onUecOutStaffEncQueryResponse() {
		uecOutStaffInfo=new UecOutStaffInfo();
		if (StrUtil.isNullOrEmpty(PlatformUtil.getCurrentUser())) {
			ZkUtil.showError("当前登录用户不存在!", "错误提示");
			return;
		} else {
			String screenName = PlatformUtil.getCurrentUser().getScreenName();
			uecOutStaffInfo.setPsnCode(screenName.toUpperCase());
		}
		if(uecOutStaffInfo!=null) {
			uecOutStaffInfo=uecOutStaffInfoManager.findUecOutStaffInfo(uecOutStaffInfo);
		}
		if (null != uecOutStaffInfo) {
			uecEnc = new UecEnc();
			uecEnc.setStaffId(uecOutStaffInfo.getOutStaffId());
			uecEnc.setNmStaffType(BaseUnitConstants.NM_OUT_STAFF_TYPE);
			encPaging = uecEnc;
			this.onQueryUecOutStaffEnc();
		}
	}




	/**
	 * 查询按钮
	 */
	public void onQueryUecOutStaffEnc() {
		this.bean.getUecOutStaffEncListboxPaging().setActivePage(0);
		this.onQueryUecOutStaffEncList();
	}



	/**
	 * 查看按钮
	 * 
	 * @throws Exception
	 * @throws InterruptedException
	 */
	public void onViewUecOutStaffEnc() throws Exception, InterruptedException {
		if (null != uecEnc) {
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("opType", "view");
			map.put("uecEnc", uecEnc);
			Window window = (Window) Executions.createComponents(
					this.editEncUrl, null,  map);
			window.doModal();
			window.addEventListener("onOK", new EventListener() {
				@Override
				public void onEvent(Event event) throws Exception {
					if (event.getData() != null) {
						//
					}
				}
			});
		} else {
			ZkUtil.showError("请选中要查看的记录", "提示信息");
		}
	}

	/**
	 * 新增按钮
	 * 
	 * @throws Exception
	 * @throws
	 */
	public void onAddUecOutStaffEnc() throws Exception {
		if (uecOutStaffInfo != null) {
			boolean isOnTrial = isOnTrial();
			if (!isOnTrial) {
				return;
			}
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("opType", "add");
			map.put("uecOutStaffInfo", uecOutStaffInfo);
			Window window = (Window) Executions.createComponents(
					this.editEncUrl, null,  map);
			window.doModal();
			window.addEventListener("onOK", new EventListener() {
				@Override
				public void onEvent(Event event) throws Exception {
					if (event.getData() != null) {
						// 新增或修改完成后，列表从数据库更新
							onQueryUecOutStaffEncList();

					}
				}
			});
		}
	}


	/**
	 * 编辑按钮
	 * 
	 * @throws Exception
	 * @throws InterruptedException
	 */
	public void onEditUecOutStaffEnc() throws Exception, InterruptedException {
		boolean isOnTrial = isOnTrial();
		if (!isOnTrial) {
			return;
		}
		if (this.bean.getUecOutStaffEncListbox().getSelectedCount() > 0) {
			uecEnc = (UecEnc) bean.getUecOutStaffEncListbox()
					.getSelectedItem().getValue();
		}
		if (uecEnc != null) {
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("opType", "edit");
			map.put("uecEnc", uecEnc);
			Window window = (Window) Executions.createComponents(
					this.editEncUrl, null, map);
			window.doModal();
			window.addEventListener("onOK", new EventListener() {
				@Override
				public void onEvent(Event event) throws Exception {
						// 新增或修改完成后，列表从数据库更新
							onQueryUecOutStaffEncList();
						}

			});
		} else {
			ZkUtil.showError("请选中要编辑的记录", "提示信息");
			return;
		}
	}


	/**
	 * 删除按钮
	 */
	public void onDelUecOutStaffEnc() {
		if (uecEnc.getId() != null) {
			ZkUtil.showQuestion("确定要删除吗?", "提示信息", new EventListener() {
				public void onEvent(Event event) throws Exception {
					Integer result = (Integer) event.getData();
					if (result == Messagebox.OK) {
						uecEnc.removeOnly();
						PubUtil.reDisplayListbox(
								bean.getUecOutStaffEncListbox(), uecEnc, "del");
						uecEnc = null;
					}
				}
			});
		} else {
			ZkUtil.showError("请选中要删除的记录", "提示信息");
			return;
		}
	}

	/**
	 * 翻页--onUecOutStaffEncListboxPaging
	 */
	public void onUecOutStaffEncListboxPaging() {
		this.onQueryUecOutStaffEncList();
	}

	/**
	 * 查询
	 */
	public void onQueryUecOutStaffEncList() {
		// 翻页查看生效数据
		if (null != encPaging) {
			PageInfo pageInfo = uecEncManager
					.queryPageInfoByUecEnc(encPaging,
							this.bean.getUecOutStaffEncListboxPaging()
									.getActivePage() + 1, this.bean
									.getUecOutStaffEncListboxPaging()
									.getPageSize());
			ListModel list = new BindingListModelList(pageInfo.getDataList(),
					true);
			this.bean.getUecOutStaffEncListbox().setModel(list);
			this.bean.getUecOutStaffEncListboxPaging().setTotalSize(
					pageInfo.getTotalCount());

		}
	}

	/**
	 * 判断是否入职
	 * 
	 * @return
	 */
	private boolean isOnTrial() {
		UecTrial qryUecTrial = UecTrial.newInstance();
		qryUecTrial.setStaffId(uecOutStaffInfo.getOutStaffId());
		qryUecTrial.setNmStaffType(BaseUnitConstants.NM_OUT_STAFF_TYPE);
		qryUecTrial = uecTrialManager.findUecTrial(qryUecTrial);
		if (null == qryUecTrial) {
			ZkUtil.showError("您未办理入职,请主管执行入职操作", "温馨提示");
			return false;
		}
		return true;
	}
}
